<?php

namespace app\modules\backend\controllers;

use app\models\Base;
use app\models\Contract;
use app\models\User;
use app\tools\Re;
use Yii;
use yii\helpers\Json;

class ContractController extends BaseController
{
    public function actionContractList(){
        $page = Yii::$app->request->post('page', 0);
        $name = Yii::$app->request->post('name', 0);
        $company = Yii::$app->request->post('company', 0);
        $query = Contract::find()->where(['status' => 1]);
        if ($name) {
            $query->andWhere(['like', 'name', $name]);
        }
        if ($company) {
            $query->andWhere(['like', 'company', $company]);
        }
        $contract_list = $query->orderBy("created_at desc")->limit(Contract::PAGE_SIZE)->offset(Contract::PAGE_SIZE*$page)->all();
        $contract_data = [];
        if ($contract_list) {
            foreach ($contract_list as $v) {
                $contract_data[] = [
                    'id' => $v->id,
                    'name' => $v->name,
                    'file' => $v->file,
                    'desc' => $v->desc,
                    'remark' => $v->remark,
                    'phones' => $v->phones,
                    'phone_arr' => explode(',', $v->phones),
                    'date_start' => date("Y-m-d", $v->start),
                    'date_end' => date("Y-m-d", $v->end),
                    'is_notice' => $v->is_notice,
                    'money' => $v->money,
                    'company' => $v->company,
                    'status' => $v->status,
                    'created_time' => date("Y-m-d H:i", $v->created_at),
                    'updated_time' => date("Y-m-d H:i", $v->updated_at),
                ];
            }
        }
        return Re::data([
            'data' => $contract_data,
            'totalPage' => $query->count(),
            'currentPage' => $page,
            'perPage' => Contract::PAGE_SIZE
        ]);
    }

    public function actionEditContract(){
        $id = Yii::$app->request->post('id', 0);
        $post = Yii::$app->request->post();
        if ($post['start']) {
          $post['start'] = strtotime(date("Y-m-d", $post['start']));
        }
        if ($post['end']) {
          $post['end'] = strtotime(date("Y-m-d", $post['end']));
        }
        $post['updated_at'] = time();
        return $this->edit(Contract::class, $id, $post);
    }

    public function actionChangeContractStatus(){
        $id = Yii::$app->request->post('id', 0);
        $status = Yii::$app->request->post('status', 0);
        $contract = Contract::findOne($id);
        if (empty($contract)) {
            return Re::data(null, '信息读取失败', Re::ERROR);
        }
        $contract->status = $status;
        if (!$contract->save()) {
            return Re::data(null, '操作失败', Re::ERROR);
        }
        return Re::data();
    }

    public function actionAdminList(){
      $list = User::find()->where(['status' => 1, 'is_admin' => 1])->andWhere("username <> 'admin'")->select("id, username, name")->all();
      return Re::data(['data' => $list]);
  }
}
